package com.lagou.edu.controller;

import com.lagou.edu.feign.EmailClient;
import com.lagou.edu.service.AuthCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/code")
public class CodeController {
    @Autowired
    private AuthCodeService codeService;

    @Autowired
    private EmailClient emailClient;

    /***
     * 生成验证码并发送到对应邮箱，成功true，失败false
     ***/
    @GetMapping("/create/{email}")
    public String create(@PathVariable @NotNull String email) {
        String code = codeService.generateCode(email);

        boolean resp = emailClient.sendEmail(email, code);

        return resp ? "true" : "false";
    }

    /***
     * 校验验证码是否正确， 0正确1错误2超时
     */
    @GetMapping("/validate/{email}/{code}")
    public String validate(@PathVariable @NotNull String email,
                           @PathVariable @NotNull String code) {
        String checkResult = codeService.validate(email, code);

        return checkResult;
    }

}
